package com.six.firstpage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six.core.entity.Banner;
import com.six.firstpage.service.BannerService;
import com.six.firstpage.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author ZZH
* @description 针对表【tb_banner(首页banner表)】的数据库操作Service实现
* @createDate 2023-02-27 10:21:18
*/
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner>
    implements BannerService{

    @Autowired
    private BannerMapper bannerMapper;

    //前端查询所有可用轮播图
    @Override
    public List<Banner> getAllBanner() {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);

        List<Banner> banners = bannerMapper.selectList(wrapper);
        //过滤掉顺序为0的,并且过滤掉顺序重复的
        ArrayList<Banner> collect = banners.stream().filter(item -> !item.getSort().equals(0)).collect(Collectors.collectingAndThen(Collectors.toCollection(
                () -> new TreeSet<>(Comparator.comparing(o -> o.getSort()))
        ), ArrayList::new));


        Collections.sort(collect);

        return collect;
    }

    //查询轮播图
    @Override
    public Page<Banner> getBannerByTitle(String title, Integer status, Integer current, Integer pageSize) {
        Page<Banner> bannerPage = new Page<>(current, pageSize);
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        if (!title.isEmpty()){
            wrapper.like("title",title);
        }
        if (status!=null){
            wrapper.eq("is_disable",status);
        }
        wrapper.eq("is_deleted",0);
        return bannerMapper.selectPage(bannerPage, wrapper);
    }

    //添加轮播图
    @Override
    public Integer addBanner(Banner banner) {
        return bannerMapper.insert(banner);
    }

    //修改轮播图
    @Override
    public Integer updateBanner(Banner banner) {

        return bannerMapper.updateById(banner);
    }


    //逻辑删除轮播图
    @Override
    public Integer delBanner(Integer id) {
        UpdateWrapper<Banner> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("is_deleted",1);
        return bannerMapper.update(null,wrapper);

    }
}




